-- 引入http模块
local http = require("resty.http")

-- 创建http客户端
local httpc = http.new()

-- request_uri函数请求mexc
local resp,err = httpc:request_uri("https://www.mexc.kr",{
    method = "GET", -- 请求方式
    query = "symbol=USDC_USDT&depth=50", --get方式请求参数
    body = "a=1&b=2", -- post方式请求参数
    path = "/open/api/v2/market/depth", -- path路径
    headers = { -- headers参数
        ["User-Agent"] = "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36"
    }
})

if not resp then
    ngx.say("request error: ",err)
    return
end

-- 获取返回的状态码
ngx.status = resp.status

if ngx.status ~= 200 then
    ngx.log(ngx.WARN,"非200状态，ngx.status: ",ngx.status)
    return
end

-- 获取遍历返回的头信息
for k,v in pairs(resp.headers) do
    if type(v) == "table" then
        ngx.log(ngx.WARN,"table: "..k,": ",table.concat(v,", "))
    else
        ngx.log(ngx.WARN,"one: "..k,": ",v)
    end
end

-- 响应体
ngx.say(resp.body)

--关闭客户端
httpc:close()